package com.xrit.sunnyday.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xrit.sunnyday.constant.MessageConstant;
import com.xrit.sunnyday.dao.CheckItemDao;
import com.xrit.sunnyday.entity.PageResult;
import com.xrit.sunnyday.entity.QueryPageBean;
import com.xrit.sunnyday.exception.SunnydayException;
import com.xrit.sunnyday.pojo.CheckItem;
import com.xrit.sunnyday.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Description: No Description
 * 解决 dubbo 2.6.0 【注意，注意，注意】
 * interfaceClass 发布出去服务的接口为这个CheckItemService.class
 * 如果没加interfaceClass, 调用No Provider 的异常，跟之前咱们使用的dubbo 2.6.2有区别
 * User: Eric
 */
@Service(interfaceClass = CheckItemService.class)
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    /**
     * 查询 所有检查项
     * @return
     */
    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }

    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //开启分页的逻辑
        //1.务必要进行配置PageHelper,我们配置在了SqlMapConfig.xml
        //2.开启分页助手
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //3.开启查询,Page是分页信息
        Page<CheckItem> page = checkItemDao.findByCondition(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteById(int id) throws SunnydayException{
        //业务层来处理检查项是否关联检查组
        int count = checkItemDao.findCountCheckItemId(id);
        //判断检查项和检查组的中间表是否存在检查项的id，id是根据前端页面传递过来的
        if(count>0){
            throw new SunnydayException("检查项关联了检查组!");
        }
        //没有关联就直接删除
        checkItemDao.deleteById(id);
    }

    @Override
    public CheckItem findById(int id) {
        return checkItemDao.findById(id);
    }

    @Override
    public void update(CheckItem checkItem) {
        checkItemDao.update(checkItem);
    }
}
